<template>
  <el-dialog v-if="load" title="批量设置客服人员" v-model="visible" align-center width="600" @close="handleClose" :close-on-click-modal="false">
    <el-form class="py-4" label-position="top">
      <el-form-item label="客服人员">
        <select-personnel class="w-full" v-model="customer_service_id" placeholder="客服人员"></select-personnel>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, toRefs } from 'vue';
import { batchUpdate } from '../api'
import { ElMessage } from 'element-plus'
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

const emit = defineEmits(['close', 'query'])

const data = reactive({
  customer_service_id: '',
  ids: []
})

const { customer_service_id, ids } = toRefs(data)

// 打开弹窗
const open = async (arr) => {
  if (arr && !arr.length) {
    ElMessage.warning('请先选择行!')
    return
  }
  if (!load.value) {
    load.value = true
    await 1
  }
  ids.value = arr
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = () => {
  loadings.submit = true
  batchUpdate({
    ids: ids.value, orderSource: {
      customer_service_id: customer_service_id.value
    }
  }).then(res => {
    ElMessage.success('操作成功')
    emit('query')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>
<style lang="scss" scoped>
:deep(.el-form-item) {
  width: 400px;
  margin: 0 auto;
}
</style>